package com.masterclient.app.businessmodule.usermodule.login.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.masterclient.app.App;
import com.masterclient.app.R;
import com.masterclient.app.base.ConfirmDialog;
import com.masterclient.app.businessmodule.usermodule.login.beans.InviterPopModel;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.masterclient.app.widget.CircleTransform;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：sean
 * 创建时间：2017/8/23 17:25
 * 功能说明：
 */

public class InvitationDialog extends Dialog {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_desc)
    TextView tvDesc;
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_invite)
    TextView tvInvite;
    @BindView(R.id.shareer_head)
    ImageView shareer_head;
    @BindView(R.id.shareer_name)
    TextView shareer_name;

    @BindView(R.id.invite_question)
    TextView invite_question;
    @BindView(R.id.allocate_reason)
    TextView allocate_reason;

    @BindView(R.id.input_invite_code)
    EditText input_invite_code;

    @BindView(R.id.gzg_layout)
    View gzg_layout;
    @BindView(R.id.inviter_layout)
    View inviter_layout;
    @BindView(R.id.input_inviter_code_layout)
    View input_inviter_code_layout;

    @BindView(R.id.title_layout)
    View title_layout;

    @BindView(R.id.input_invite_code_next_layout)
    View input_invite_code_next_layout;
    @BindView(R.id.iv_head_next)
    ImageView iv_head_next;
    @BindView(R.id.tv_name_next)
    TextView tv_name_next;
    @BindView(R.id.tv_invite_next)
    TextView tv_invite_next;

    @BindView(R.id.skip_login)
    TextView skip_login;

    Context context;

    public InvitationDialog(@NonNull Context context, OnChooseListerner onChooseListerner) {
        super(context, R.style.MyDialog);
        this.onChooseListerner = onChooseListerner;
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_invitation);
        ButterKnife.bind(this);

    }

    @Override
    protected void onStart() {
        super.onStart();
        Window window = getWindow();
        WindowManager.LayoutParams windowparams = window.getAttributes();
        windowparams.width = ScreenUtils.getScreenWidth();
        window.setGravity(Gravity.CENTER);
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setAttributes(windowparams);
    }

    public void setOnChooseListerner(OnChooseListerner onChooseListerner) {
        this.onChooseListerner = onChooseListerner;
    }

    InviterPopModel inviterPopModel;
    boolean hasInputInviteCode = false;
    public void setData(InviterPopModel inviterPopModel){
        this.inviterPopModel = inviterPopModel;
        gzg_layout.setVisibility(View.GONE);
        inviter_layout.setVisibility(View.GONE);
        input_inviter_code_layout.setVisibility(View.GONE);
        title_layout.setVisibility(View.GONE);
        input_invite_code_next_layout.setVisibility(View.GONE);
        if(inviterPopModel == null){
            //Toast.makeText(context,"网络异常",Toast.LENGTH_LONG).show();
            Toast toast = Toast.makeText(context,"网络异常", Toast.LENGTH_LONG);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
            dismiss();
            return;
        }
        if(inviterPopModel.isCan_skip()){
            skip_login.setVisibility(View.VISIBLE);
        }else {
            skip_login.setVisibility(View.GONE);
        }
        String pop_type = inviterPopModel.getPop_type();
        if("1".equals(pop_type)){
            gzg_layout.setVisibility(View.VISIBLE);
            title_layout.setVisibility(View.VISIBLE);
            if(!TextUtils.isEmpty(inviterPopModel.getSharer_avatar())){
                App.picasso.load(inviterPopModel.getSharer_avatar()).transform(new CircleTransform()).error(R.mipmap.user_head_default_icon0).placeholder(
                        R.mipmap.user_head_default_icon0).into(shareer_head);
            }
            shareer_name.setText(inviterPopModel.getSharer_nickname() + "邀请您加入中华大师汇");
        }else if("2".equals(pop_type)){
            input_inviter_code_layout.setVisibility(View.VISIBLE);
            title_layout.setVisibility(View.VISIBLE);
            shareer_name.setText("感谢您加入中华大师汇");
            hasInputInviteCode = true;
        }else if("3".equals(pop_type)){
            gzg_layout.setVisibility(View.VISIBLE);
            title_layout.setVisibility(View.VISIBLE);
            shareer_name.setText("感谢您加入中华大师汇");
        }else if("4".equals(pop_type)){
            inviter_layout.setVisibility(View.VISIBLE);
            title_layout.setVisibility(View.VISIBLE);
            if(!TextUtils.isEmpty(inviterPopModel.getSharer_avatar())){
                App.picasso.load(inviterPopModel.getSharer_avatar()).transform(new CircleTransform()).error(R.mipmap.user_head_default_icon0).placeholder(
                        R.mipmap.user_head_default_icon0).into(shareer_head);
            }
            shareer_name.setText(inviterPopModel.getSharer_nickname() + "邀请您");
            if(!TextUtils.isEmpty(inviterPopModel.getInviter_avatar())){
                App.picasso.load(inviterPopModel.getInviter_avatar()).transform(new CircleTransform()).error(R.mipmap.user_head_default_icon0).placeholder(
                        R.mipmap.user_head_default_icon0).into(ivHead);
            }
            tvName.setText(inviterPopModel.getInviter_nickname());
            tvInvite.setText("邀请码：" + inviterPopModel.getInviter_code());
            invite_question.setVisibility(View.GONE);
            tvTitle.setText("点击确认成为以下用户的粉丝");
        }else if("5".equals(pop_type)){
            inviter_layout.setVisibility(View.VISIBLE);
            title_layout.setVisibility(View.VISIBLE);
            shareer_name.setText("感谢您加入中华大师汇");
            if(!TextUtils.isEmpty(inviterPopModel.getInviter_avatar())){
                App.picasso.load(inviterPopModel.getInviter_avatar()).transform(new CircleTransform()).error(R.mipmap.user_head_default_icon0).placeholder(
                        R.mipmap.user_head_default_icon0).into(ivHead);
            }
            tvName.setText(inviterPopModel.getInviter_nickname());
            tvInvite.setText("邀请码：" + inviterPopModel.getInviter_code());
            invite_question.setVisibility(View.VISIBLE);
            tvTitle.setText("我们为您分配了一个邀请人");
            allocate_reason.setText(inviterPopModel.getAllocate_reason());
        }else if("10".equals(pop_type)){
            input_invite_code_next_layout.setVisibility(View.VISIBLE);
            if(!TextUtils.isEmpty(inviterPopModel.getInviter_avatar())){
                App.picasso.load(inviterPopModel.getInviter_avatar()).transform(new CircleTransform()).error(R.mipmap.user_head_default_icon0).placeholder(
                        R.mipmap.user_head_default_icon0).into(iv_head_next);
            }
            tv_name_next.setText(inviterPopModel.getInviter_nickname());
            tv_invite_next.setText("邀请码：" + inviterPopModel.getInviter_code());
        }
    }

    private OnChooseListerner onChooseListerner;

    @OnClick({R.id.tv_cancel, R.id.tv_confirm,R.id.invite_question,R.id.skip_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
                //

                show_second_confirm_dialog("点击取消将无法登陆也无法绑定\n" +
                        "确认取消吗？");

                break;
            case R.id.tv_confirm:
                if("2".equals(inviterPopModel.getPop_type())){
                    String invite_code = input_invite_code.getText().toString().trim();
                    if(TextUtils.isEmpty(invite_code)){
                        Toast toast = Toast.makeText(context,"请输入邀请码", Toast.LENGTH_LONG);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.show();
                        //Toast.makeText(context,"请输入邀请码",Toast.LENGTH_LONG).show();
                        return;
                    }
                    onChooseListerner.get_by_invite_code(invite_code);
                }else{
                    onChooseListerner.confirm();
                }
                break;
            case R.id.invite_question:
                if(allocate_reason.getVisibility() == View.VISIBLE){
                    allocate_reason.setVisibility(View.GONE);
                }else{
                    allocate_reason.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.skip_login:
                onChooseListerner.skip_login();
                break;
        }
    }

    ConfirmDialog confirmDialog;
    private void show_second_confirm_dialog(String content) {
        confirmDialog = new ConfirmDialog(context,content,"确认取消","继续绑定");
        confirmDialog.setOnChooseListerner(new ConfirmDialog.OnChooseListerner() {
            @Override
            public void cancel() {
                confirmDialog.dismiss();
                if(hasInputInviteCode && input_inviter_code_layout.getVisibility() != View.VISIBLE){
                    inviterPopModel.setPop_type("2");
                    setData(inviterPopModel);
                }else{
                    onChooseListerner.cancel();
                }

            }

            @Override
            public void confirm() {
                confirmDialog.dismiss();
                confirmDialog = null;
            }
        });
        confirmDialog.show();
    }

    public interface OnChooseListerner {
        public void cancel();
        public void get_by_invite_code(String invite_code);
        public void confirm();
        public void skip_login();
    }
}
